from pytest_assume.plugin import assume
from utils.extract_data_handler import extract_by_object
from utils.log_handler import logger
from utils.db_handler import db


def assert_result(check_data, res):
    for check_type, check_value in check_data.items():
        if check_type in ["equals", "sql"]:
            for i in check_value:
                expression = i[0]
                if check_type == "sql":
                    actual_value = db.fetch_one(expression).get(i[2], None)
                else:
                    actual_value = extract_by_object(res, expression)
                logger.info(f"开始断言{check_type}-预期值：{i[1]}，实际值：{actual_value}.....")
                result = assume(actual_value == i[1], f"预期值：{actual_value}不等于实际值：{i[1]}")
                if result:
                    logger.info(f"{check_type}[{i[1]}, {actual_value}]断言成功.....")
                else:
                    logger.error(f"错误信息为：预期值：{actual_value}不等于实际值：{i[1]}")
        elif check_type == "contains":
            for i in check_value:
                logger.info(f"开始断言{check_type}-关键字：{i}.....")
                result = assume(i in res.text, f"响应文本不包含{i}")
                if result:
                    logger.info(f"{check_type}-关键字：{i}包含在响应文本中，断言成功.....")
                else:
                    logger.error(f"错误信息为：响应文本不包含{i}")
